Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created July 5, 2025 23:25
Shared via mypy Playground
from __future__ import annotations
from dataclasses import dataclass
from typing import (
Any,
Callable,
Protocol,
overload,
)
@mypy-play
mypy-play / main.py
Created July 5, 2025 23:21
Shared via mypy Playground
from __future__ import annotations
from dataclasses import dataclass
from typing import (
Any,
Callable,
Protocol,
overload,
)
@mypy-play
mypy-play / main.py
Created July 5, 2025 23:18
Shared via mypy Playground
from __future__ import annotations
from dataclasses import dataclass
from typing import (
Any,
Protocol,
overload,
)
@mypy-play
mypy-play / main.py
Created July 5, 2025 19:54
Shared via mypy Playground
from typing import Iterator
def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
@mypy-play
mypy-play / main.py
Created July 5, 2025 15:49
Shared via mypy Playground
from typing import List, Set
class Wizard:
def __init__(self, name: str, expert: str, spells: List[int]):
self.name = name
self.spells = spells
self.expert = expert
def average_spell_power(self):
return sum(self.spells) / len(self.spells)
@mypy-play
mypy-play / main.py
Created July 5, 2025 12:28
Shared via mypy Playground
from typing import List, Set
class Wizard:
def __init__(self, name, expert, spells):
self.name = name
self.spells = spells
self.expert = expert
def average_spell_power(self):
return sum(self.spells) / len(self.spells)
@mypy-play
mypy-play / main.py
Created July 5, 2025 00:30
Shared via mypy Playground
from typing import Iterator
def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
@mypy-play
mypy-play / main.py
Created July 4, 2025 14:27
Shared via mypy Playground
from typing import Never
class A:
x: int
class B:
x: str
class X(A, B):
@property
def x(self) -> Never: raise Exception
@mypy-play
mypy-play / main.py
Created July 4, 2025 14:23
Shared via mypy Playground
from typing import Never
class A:
def f(self) -> int: return 0
class B:
def f(self) -> str: return ""
class X(A, B):
def f(self) -> Never: raise Exception
@mypy-play
mypy-play / main.py
Created July 4, 2025 11:43
Shared via mypy Playground
# Python implementation of the algorithm from "Lambda-Lifting in Quadratic Time" by Danvy and Schultz
from __future__ import annotations
import io
import sys
import types
from collections.abc import Mapping
from typing import Generic, TypeVar